/**
 * 
 */
package com.alkandros.linesmash.ui;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.alkandros.linesmash.R;
import com.alkandros.linesmash.adapter.VenuesAdapter;
import com.alkandros.linesmash.constants.AppConstants;
import com.alkandros.linesmash.location.ILocationCallBack;
import com.alkandros.linesmash.manager.APIManager;
import com.alkandros.linesmash.manager.AlertManager;
import com.alkandros.linesmash.models.Venues;

/**
 * @author Jiju Induchoodan
 *
 */
public class VenuesPage extends BaseActivity implements ILocationCallBack {

	FetchVenues fetch;
	private ListView list;
	VenuesAdapter adapter;
	private Intent intent;
	ArrayList<Venues> venues = new ArrayList<Venues>();
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dining_locations);
		initialiseUI();
	}
	
	private void initialiseUI()
	{
		list    = (ListView)findViewById(R.id.list);
		setHeading(R.string.venues_near_you);
		setLeftButton();
		setRightButton();
		
		 
		
	     fetch= new FetchVenues();
		fetch.execute();
		
		list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				intent = new Intent(VenuesPage.this,DiningLocations.class);
				intent.putExtra("venue",venues.get(arg2));
				startActivity(intent);
					 
			}
		});
	}
	
	class FetchVenues extends AsyncTask<Void, Void, Void>
	{
		private APIManager manager;
		private boolean isException=false;
		private String response;
		private String [] names= {"",""};
		private String [] values= {"",""};
		
		
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			progress.dismiss();
			if(isException)
			{
				AlertManager.shorttoastMessage(VenuesPage.this, getString(R.string.common_error_message));
			}
			else
			{
				if(response.contains(ERROR_CODE))
				{
					AlertManager.shorttoastMessage(VenuesPage.this, getString(R.string.common_error_message));
				}
				else
				{
					try
					{
						 venues = new ArrayList<Venues>();
						JSONObject obj = new JSONObject(response);
						JSONArray array = obj.getJSONArray(DATA);
						for (int i = 0; i < array.length(); i++) 
						{
							Venues model = new Venues();
							JSONObject postObject = array.getJSONObject(i).getJSONObject(VENUE);
							model.setTitle(postObject.getString(TITLE));
							model.setId(postObject.getString(ID));
					//		model.setDistance(array.getJSONObject(i).getString(DISTANCE));
							venues.add(model);
						}
							adapter = new VenuesAdapter(context, 0);
							adapter.setVenuesData(venues);
							list.setAdapter(adapter); 
					}
					catch(Exception e)
					{
						Log.e("", "Exception"+e.getMessage());
						AlertManager.shorttoastMessage(VenuesPage.this, getString(R.string.common_error_message));
					}
					
				}
			}
		}
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progress.show();
			manager = new APIManager(URL_VENUES);
		}

		@Override
		protected Void doInBackground(Void... params) {
			try 
			{
				response= manager.commonPostData(URL_VENUES+AppConstants.APP_LATITUDE+"/"+AppConstants.APP_LONGITUDE,names,values);
			}
			catch (Exception e)
			{
				e.printStackTrace();
				isException=true;
			}
			return null;
		}
		
	}

	/* (non-Javadoc)
	 * @see com.alkandros.linesmash.location.ILocationCallBack#setResult(java.lang.String)
	 */
	@Override
	public void setResult(String address) {
		// TODO Auto-generated method stub
		
	}

	

	
}
